/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.solr.local;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * solr更新操作的参数封装类
 *
 * @see AbstractSearcher#update(SolrUpdateParam)
 * @author yoara
 */
public class SolrUpdateParam {
    private String idName;
    private String idValue;

    public String getIdName() {
        return idName;
    }

    public String getIdValue() {
        return idValue;
    }

    public List<UpdateParam> getParamList() {
        return paramList;
    }

    public SolrUpdateParam(String idName, String idValue) {
        this.idName = idName;
        this.idValue = idValue;
    }

    private List<UpdateParam> paramList = new ArrayList<>();

    public void addParam(OperatorType type,String field,Object value){
        paramList.add(new UpdateParam(type,field,value));
    }

    public static class UpdateParam{
        private String field;
        private OperatorType operatorType;
        private Object value;

        public UpdateParam(OperatorType operatorType, String field, Object value) {
            this.field = field;
            this.operatorType = operatorType;
            this.value = value;
        }

        public String getField() {
            return field;
        }

        public Map<String,Object> pickUpdateInfo(){
            Map<String,Object> info = new HashMap<>();
            info.put(operatorType.name(),value);
            return info;
        }
    }

    /**
     * solr支持三种类型的原子更新：
     * set - to set a field.
     * add - to add to a multi-valued field.
     * inc - to increment a field.
     * 其中set可以是单值的也可以是multifiled， add  针对multi-field ,inc 对应数值类型
     */
    public enum OperatorType{
        add,
        set,
        inc
    }
}
